package com.wptk.filemanager.ui.clean;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wptk.filemanager.App;
import com.wptk.filemanager.R;
import com.wptk.filemanager.ad.utils.AnalyticsUtils;
import com.wptk.filemanager.adapter.ScanAdapter;
import com.wptk.filemanager.adapter.ThridAppAdapter;
import com.wptk.filemanager.model.FileDataManager;
import com.wptk.filemanager.model.clean.ThridApp;
import com.wptk.filemanager.ui.base.InterruptActivity;
import com.hz.sdka.ada.AdListenera;
import com.hz.sdka.commona.bean.HZAdError;

import java.util.List;

public class ScanResultActivity extends InterruptActivity {

    public final static String NUMBER = "number";

    private TextView scoreTv;
    private RecyclerView listViewRv;
    private ScanAdapter scanAdapter;
    private ThridAppAdapter appsAdapter;
    private CheckBox cbAll;
    private int selectAppSize;
    private List<ThridApp> thridAppList;
    private Button boostBtn;
    private String numberStr;
    private TextView perTv;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_boost_scan_result;
    }

    @Override
    protected void initView() {
        toolbar = findViewById(R.id.toolbar);
        scoreTv = findViewById(R.id.score_tv);
        perTv = findViewById(R.id.per_tv);
        listViewRv = findViewById(R.id.listview_rv);
        cbAll = (CheckBox) findViewById(R.id.cb_all);
        boostBtn = (Button) findViewById(R.id.boost_btn);
    }

    @Override
    protected void initEvent() {

        type = getIntent().getIntExtra(TYPE, 0);
        numberStr = getIntent().getStringExtra(NUMBER);
        thridAppList = FileDataManager.getInstance().getScanResultList();

        scoreTv.setText(numberStr);
        switch (type){
            case TYPE_BOOST:
                AnalyticsUtils.INSTANCE.logEvent("saomiaojg_js");
                toolbar.setTitle(R.string.phone_boost);
                perTv.setText("%");
                boostBtn.setText(getString(R.string.speed_up));
                break;
            case TYPE_COOLER:
                AnalyticsUtils.INSTANCE.logEvent("saomiaojg_jw");
                toolbar.setTitle(R.string.phone_cooler);
                perTv.setText("℃");
                boostBtn.setText(getString(R.string.cooling));
                break;
            case TYPE_BATTERY:
                AnalyticsUtils.INSTANCE.logEvent("saomiaojg_sd");
                toolbar.setTitle(R.string.battery_saver);
                perTv.setText("apps");
                boostBtn.setText(getString(R.string.power_saving));
                if(App.thridAppList != null )
                    thridAppList = App.thridAppList;
                break;
        }

        listViewRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        selectAppSize = thridAppList.size();

        cbAll.setChecked(true);
        cbAll.setText(selectAppSize + "/" + selectAppSize);

        cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    selectAppSize = thridAppList.size();
                }else{
                    selectAppSize = 0;
                }

                for(ThridApp fixApp : thridAppList){
                    fixApp.setCheck(isChecked);
                }

                appsAdapter.setDatas(thridAppList);
                listViewRv.setAdapter(appsAdapter);

                cbAll.setText( selectAppSize + "/" + thridAppList.size());

            }
        });

        boostBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ScanResultActivity.this, AnimationActivity.class);
                switch (type){
                    case TYPE_BOOST:
                        intent.putExtra(AnimationActivity.TYPE, AnimationActivity.TYPE_BOOST);
                        break;
                    case TYPE_COOLER:
                        intent.putExtra(AnimationActivity.TYPE, AnimationActivity.TYPE_COOLER);
                        break;
                    case TYPE_BATTERY:
                        intent.putExtra(AnimationActivity.TYPE, AnimationActivity.TYPE_BATTERY);
                        break;
                }
                startActivity(intent);
                finish();
            }
        });

        appsAdapter = new ThridAppAdapter();
        appsAdapter.setDatas(thridAppList);
        appsAdapter.setOnCheckChangedListener(new ThridAppAdapter.OnCheckChangedListener() {
            @Override
            public void onCheckedChanged(boolean  isChecked, String packageName) {

                if(isChecked){
                    selectAppSize++;
                }else{
                    selectAppSize--;
                }

                cbAll.setText(selectAppSize + "/" + thridAppList.size());

                if(selectAppSize == thridAppList.size()){
                    cbAll.setChecked(true);
                }else if(selectAppSize == 0){
                    cbAll.setChecked(false);
                }
            }
        });
        listViewRv.setAdapter(appsAdapter);

    }
}
